﻿using System;
using Pixster.Slideshow.ShaderEffects;
using System.Collections.Generic;

namespace Pixster.Slideshow.BusinessObjects
{
    public sealed class TransitionEffectsManager
    {
        private static TransitionEffectsManager _instance;
        private readonly List<TransitionEffect> _register;
        private readonly Random _rand = new Random();

        public event EventHandler IndexChanged;

        public static TransitionEffectsManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new TransitionEffectsManager();

                return _instance;
            }
        }

        public TransitionEffect CurrentEffect
        {
            get
            {
                var effect = this._register[this.CurrentIndex];
                if (effect is RandomTransition)
                    (effect as RandomTransition).RandomSeed = this._rand.NextDouble();

                return effect;
            }
        }

        public int CurrentIndex { get; private set; }

        public void MoveNext()
        {
            this.CurrentIndex += 1;
            this.CurrentIndex %= this._register.Count;
            this.OnIndexChanged(EventArgs.Empty);
        }

        public string[] GetNames
        {
            get
            {
                var names = new List<string>();
                foreach (var effect in this._register)
                    names.Add(effect.Name);
                return names.ToArray();
            }
        }

        private void OnIndexChanged(EventArgs e)
        {
            var temp = this.IndexChanged;
            if (temp != null)
                temp(this, e);
        }

        private TransitionEffectsManager()
        {
            _register = new List<TransitionEffect> {
                                                       new FadeTransition(),
                                                       new BlindsTransition(),
                                                       new PixelateTransition()
                                                   };
        }
    }
}